package com.farm.chain;

// 责任链模式使用示例
public class ChainOfResponsibilityDemo {
    public static void main(String[] args) {
        String  machineName = "Machine#1";
        // 创建处理器链
        EmergencyHandler obstacleHandler = new ObstacleHandler();
        EmergencyHandler lowFuelHandler = new LowFuelHandler();
        EmergencyHandler systemFaultHandler = new SystemFaultHandler();

        obstacleHandler.setNextHandler(lowFuelHandler);
        lowFuelHandler.setNextHandler(systemFaultHandler);

        // 创建各种紧急请求
        EmergencyRequest obstacleRequest = new EmergencyRequest("OBSTACLE", "前方检测到障碍物");
        EmergencyRequest lowFuelRequest = new EmergencyRequest("LOW_FUEL", "油量低于15%");
        EmergencyRequest systemFaultRequest = new EmergencyRequest("SYSTEM_FAULT", "导航系统故障");
        EmergencyRequest unknownRequest = new EmergencyRequest("UNKNOWN", "未知错误");

        // 处理请求
        obstacleHandler.handleRequest(obstacleRequest,machineName);
        System.out.println("-------------------");
        obstacleHandler.handleRequest(lowFuelRequest,machineName);
        System.out.println("-------------------");
        obstacleHandler.handleRequest(systemFaultRequest,machineName);
        System.out.println("-------------------");
        obstacleHandler.handleRequest(unknownRequest,machineName);
    }
}